/* 
 * Copyright (C) 2015 fede
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package Entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import org.eclipse.persistence.annotations.Mutable;

/**
 *
 * @author fede
 */
@Entity
@NamedQueries({
    @NamedQuery(name = Event.FIND_ALL_PUBLIC, query = "SELECT e FROM Event e WHERE (e.owner.email <> :id) AND  (e.public_bool = :pubBool)"),
    @NamedQuery(name = Event.FIND_ALL_CALENDAR, query = "SELECT e FROM Calendar c JOIN Event e WHERE (e.owner.email= c.owner.email) "
            + "                                             AND (e.owner.email <> :id) "
            + "                                             AND (c.public_bool = :pubBoolCal) "
            + "                                             AND (e.public_bool = :pubBoolEv)"),
    @NamedQuery(name = Event.FIND_ALL_INVITED, query = "SELECT e FROM Event e WHERE e.guest= :id"),})
public class Event implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    private String id;

    private String title;

    @Mutable
    @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
    private Date startEventDate;

    @Mutable
    @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
    private Date endEventDate;
    private String location;
    private String description;
    private Boolean all_day;
    private Boolean public_bool;
    private Boolean indoor_bool;

    /**
     * find all public event
     */
    public static final String FIND_ALL_PUBLIC = "Event.findAllPublic";

    /**
     * find all calendar of owner
     */
    public static final String FIND_ALL_CALENDAR = "Event.findAllCalendar";

    /**
     * find all event where a user are invited
     */
    public static final String FIND_ALL_INVITED = "Event.findAllInvited";

    //association with WeatherForecast
    @OneToOne(cascade = CascadeType.REMOVE)
    private WeatherForecast weatherForecast;

    //association with RegisteredUser
    //owner of the event
    @OneToOne
    private RegisteredUser owner;

    //association with RegisteredUser
    //invited user (guest) for the event
    @ManyToMany(cascade = CascadeType.DETACH)
    private List<RegisteredUser> guest;

    /**
     * this method allow to get the owner of the current event
     *
     * @return owner of event
     */
    public RegisteredUser getOwner() {
        return owner;
    }

    /**
     * this method allow to set the owner of the current event
     *
     * @param owner the RegisteredUser to set as owner
     */
    public void setOwner(RegisteredUser owner) {
        this.owner = owner;
    }

    /**
     * this method allow to get the wheather forecast of the current event
     *
     * @return WeatherForecast object
     */
    public WeatherForecast getWeatherForecast() {
        return weatherForecast;
    }

    /**
     * this method allow to set the wheather forecast for the current event
     *
     * @param weatherForecast the weather to set
     */
    public void setWeatherForecast(WeatherForecast weatherForecast) {
        this.weatherForecast = weatherForecast;
    }

    /**
     * this method allow to get all invited user for the current event
     *
     * @return the invited users as a list
     */
    public List<RegisteredUser> getGuest() {
        return guest;
    }

    /**
     * this method allow to set all invited user for the current event
     *
     * @param Guest list of RegisteredUser invited to an event
     */
    public void setGuest(List<RegisteredUser> Guest) {
        this.guest = Guest;
    }

    /**
     * this method allow to get title of the current event
     *
     * @return String of title
     */
    public String getTitle() {
        return title;
    }

    /**
     * this method allow to get title of the current event
     *
     * @param title string
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * this method allow to get the start date of the current event
     *
     * @return Date of start
     */
    public Date getStartEventDate() {
        return startEventDate;
    }

    /**
     * this method allow to set the start date for the current event
     *
     * @param startEventDate Date to start
     */
    public void setStartEventDate(Date startEventDate) {
        this.startEventDate = startEventDate;
    }

    /**
     * this method allow to get the end date of the current event
     *
     * @return Date of end
     */
    public Date getEndEventDate() {
        return endEventDate;
    }

    /**
     * this method allow to set the end date for the current event
     *
     * @param endEventDate Date of end
     */
    public void setEndEventDate(Date endEventDate) {
        this.endEventDate = endEventDate;
    }

    /**
     * this method allow to get the location of the current event
     *
     * @return location as a string
     */
    public String getLocation() {
        return location;
    }

    /**
     * this method allow to set the location for the current event
     *
     * @param location as a string
     */
    public void setLocation(String location) {
        this.location = location;
    }

    /**
     * this method allow to get the description of the current event
     *
     * @return description as a String
     */
    public String getDescription() {
        return description;
    }

    /**
     * this method allow to set the description for the current event
     *
     * @param description as a String
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * this method get the public or private visibility of event
     *
     * @return boolean value
     */
    public Boolean getPublic_bool() {
        return public_bool;
    }

    /**
     * this method set the public or private visibility to event
     *
     * @param public_bool public=true, private=false
     */
    public void setPublic_bool(Boolean public_bool) {
        this.public_bool = public_bool;
    }

    /**
     * this method get the indoor or outdoor for event
     *
     * @return boolean value
     */
    public Boolean getIndoor_bool() {
        return indoor_bool;
    }

    /**
     * this method set the indoor or outdoor for event
     *
     * @param indoor_bool indoor=true, outdoor=false
     */
    public void setIndoor_bool(Boolean indoor_bool) {
        this.indoor_bool = indoor_bool;
    }

    /**
     * this method get the all day parameter of event
     *
     * @return boolean value
     */
    public Boolean getAll_day() {
        return all_day;
    }

    /**
     * this method set the all day parameter for event
     *
     * @param all_day boolean value
     */
    public void setAll_day(Boolean all_day) {
        this.all_day = all_day;
    }

    /**
     * this method get the id of event
     *
     * @return is as a string
     */
    public String getId() {
        return id;
    }

    /**
     * this method set the id for event
     *
     * @param id as a string
     */
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 17 * hash + Objects.hashCode(this.id);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Event other = (Event) obj;
        if (!Objects.equals(this.id, other.id)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Event{" + "id=" + id + ", title=" + title + ", startEventDate=" + startEventDate + ", endEventDate=" + endEventDate + ", location=" + location + ", description=" + description + ", all_day=" + all_day + ", public_bool=" + public_bool + ", indoor_bool=" + indoor_bool + ", weatherForecast=" + weatherForecast + ", owner=" + owner + ", guest=" + guest + '}';
    }

}
